import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cwhisky/common/style/colors.dart';
import 'package:flutter_cwhisky/component/app_bar/tools_bar.dart';
import 'package:flutter_cwhisky/component/base/base_state_less_view.dart';
import 'package:flutter_cwhisky/component/image_view.dart';
import 'package:flutter_cwhisky/component/ink/ink_well_custom.dart';
import 'package:flutter_cwhisky/component/text/edit_text.dart';
import 'package:flutter_cwhisky/component/text_view.dart';
import 'package:flutter_cwhisky/gen/assets.gen.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../component/text/app_button.dart';
import 'login_logic.dart';

class LoginPage extends BaseStateLessView<LoginLogic> {
  final logic = Get.put(LoginLogic());

  LoginPage({super.key});

  @override
  Widget buildContent(BuildContext context) {
    return Scaffold(
      appBar: ToolsBar(
        title: '',
        actions: [],
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Column(
          mainAxisSize: MainAxisSize.max,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [_buildLogo(), _buildInput(), _buildLoginButton()],
        ),
      ),
    );
  }

  Widget _buildLogo() {
    return Container(
      margin: EdgeInsets.only(top: 70.h),
      child: Image.asset(
        Assets.images.loginLogo.path,
        height: 80.h,
      ),
    );
  }

  _buildInput() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        children: [
          Container(
            height: 40.h,
            margin: EdgeInsets.only(top: 12.h),
            child: EditText(
              icon: ImageView(
                url: Assets.images.loginPhone.path,
                width: 14.r,
              ),
              hintText: "请输入您的手机号码",
              inputFormatters: [
                LengthLimitingTextInputFormatter(11),
                FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
              ],
              keyboardType: TextInputType.phone,
            ),
          ),
          Container(
            height: 40.h,
            margin: EdgeInsets.only(top: 12.h),
            child: Row(
              children: [
                Expanded(
                    child: EditText(
                  icon: ImageView(
                    url: Assets.images.loginMsg.path,
                    width: 18.r,
                  ),
                  hintText: "图片验证码",
                  keyboardType: TextInputType.multiline,
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(6),
                    FilteringTextInputFormatter.allow(RegExp("^[a-z0-9A-Z]+")), //只允许输入数字，字母
                  ],
                )),
                Container(
                  child: Image.memory(
                    base64Decode(controller.imageCode!),
                    height: 40.h,
                  ),
                )
              ],
            ),
          ),
          Container(
            height: 40.h,
            margin: EdgeInsets.only(top: 12.h),
            child: Row(
              children: [
                Expanded(
                    child: EditText(
                  icon: ImageView(
                    url: Assets.images.loginPassword.path,
                    width: 18.r,
                  ),
                  hintText: "请输入验证码",
                  obscureText: true,
                  maxLines: 1,
                  keyboardType: TextInputType.text,
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(6),
                    FilteringTextInputFormatter.allow(RegExp("^[a-z0-9A-Z]+")), //只允许输入数字，字母
                  ],
                )),
                InkWellCustom(
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 6, horizontal: 6),
                    margin: EdgeInsets.only(left: 24.w),
                    child: TextView(
                      text: controller.sendCodeText,
                    ),
                  ),
                  onTap: () {
                    controller.getCaptcha();
                  },
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  _buildLoginButton() {
    return Container(
      margin: EdgeInsets.only(top: 50.h, left: 16.w, right: 16.w),
      child: AppButton(
        colorStyle: ColorStyle(
          color: AppColors.white,
          bgColor: AppColors.red,
        ),
        text: '登录',
        onPressed: () {
          
        },
      ),
    );
  }
}
